package com.hsd.znsh.service.impl;

import com.hsd.znsh.entity.ProductType;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.pojo.output.TreeNode;
import com.hsd.znsh.repository.ProductTypeRepository;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.service.ProductTypeService;
import com.hsd.znsh.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-26 14:19:21
 */
@Service
public class ProductTypeServiceImpl extends BaseServiceImpl implements ProductTypeService{

    @Autowired
    private ProductTypeRepository productTypeRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public Map<Integer, TreeNode> getProductTypeList() {
        List<ProductType> list=productTypeRepository.findByDeleted(DeletedStatusEnum.NOT_DELETED.getCode());
        Map<Integer, TreeNode> nodelist = new LinkedHashMap<Integer, TreeNode>();
        for (ProductType productType : list) {
            TreeNode node = new TreeNode();
            node.setText(productType.getName());
            node.setId(productType.getId());
            node.setParentId(productType.getParentId());
            nodelist.put(node.getId(), node);
        }
        // 构造树形结构
        return nodelist;
    }

    @Override
    public ProductType getProductTypeInfo(Integer id) {
        ProductType productType=productTypeRepository.findOne(id);
        return productType;
    }

    @Override
    public ProductType updateProductType(ProductType productType) {
        ProductType target=productTypeRepository.findOne(productType.getId());
        BeanUtil.copyPropertiesIgnoreNull(productType,target);
        return productTypeRepository.save(target);
    }

    @Override
    public ProductType addProductType(ProductType productType) {
        return productTypeRepository.save(productType);
    }

    @Override
    public void deletedProductType(Integer id) {
        ProductType productType=productTypeRepository.findOne(id);
        productType.setDeleted(DeletedStatusEnum.DELETED.getCode());
        productTypeRepository.save(productType);
    }
}
